package com.k.i18n.config;

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.LocaleChangeInterceptor;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import java.util.Locale;

/**
 * @author k
 */
@Configuration
public class I18nConfig implements WebMvcConfigurer{
    private static final Locale DEFAULT_LANG = Locale.SIMPLIFIED_CHINESE;
    @Bean
    public MessageSource messageSource(){
        // 支持 properties  xml
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:static/i18n/prop/strings");
        messageSource.setDefaultEncoding("UTF-8");
        messageSource.setUseCodeAsDefaultMessage(true);
        messageSource.setCacheSeconds(60 * 60);
        return messageSource;
    }
    // Nacos
//    @Bean
//    public MessageSource messageSource(ConfigService configService) throws Exception {
//        ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
//        // 设置国际化资源文件的基础名（不包含.properties后缀）
//        messageSource.setBasenames("messages");
//        messageSource.setUseCodeAsDefaultMessage(true);
//        // 设置缓存周期，如果需要动态更新，则设置为-1
//        messageSource.setCacheSeconds(60 * 60); // 1 hour
//        // 从Nacos配置中心动态获取i18n资源
//        Properties properties = new Properties();
//        properties.put("spring.cloud.nacos.config.extension-configs[0].data-id", "messages.properties");
//        properties.put("spring.cloud.nacos.config.extension-configs[0].group", "DEFAULT_GROUP");
//        properties.put("spring.cloud.nacos.config.extension-configs[0].refresh", "true");
//        ConfigService.getAppName();
//        configService.addListener("messages.properties", "DEFAULT_GROUP", new Listener() {
//            @Override
//            public Executor getExecutor() {
//                return null;
//            }
//
//            @Override
//            public void receiveConfigInfo(String configInfo) {
//                Properties properties = new Properties();
//                properties.load(new StringReader(configInfo));
//                messageSource.setProperties(properties);
//            }
//        });
//        return messageSource;
//    }

    @Bean
    public LocaleResolver localeResolver(){
        return getSessionLocaleResolver();
    }

    /**
     * 找到 LocaleResolver 根据 url ?lang=zh_CN 设置
     * @return LocaleChangeInterceptor
     */
    @Bean
    public LocaleChangeInterceptor localeChangeInterceptor(){
        LocaleChangeInterceptor interceptor = new LocaleChangeInterceptor();
        interceptor.setParamName("lang");
        return interceptor;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(localeChangeInterceptor());
    }

    /**
     * Session 获取 Locale
     * @return LocaleResolver
     */
    private LocaleResolver getSessionLocaleResolver(){
        SessionLocaleResolver resolver = new SessionLocaleResolver();
        resolver.setDefaultLocale(DEFAULT_LANG);
        return resolver;
    }

    /**
     * Cookie 获取 Locale
     * @return LocaleResolver
     */
    private LocaleResolver getCookieLocaleResolver(){
        CookieLocaleResolver resolver = new CookieLocaleResolver();
        resolver.setCookieName("lang");
        resolver.setDefaultLocale(DEFAULT_LANG);
        resolver.setCookieMaxAge(3600);
        return resolver;
    }

    /**
     * 请求头 获取 Locale
     * Accept-Language 找不到会使用默认语言
     * 1.header Accept-Language
     * 2.配置
     * 3.默认
     * @return LocaleResolver
     */
    private LocaleResolver getAcceptHeaderLocaleResolver(){
        AcceptHeaderLocaleResolver resolver = new AcceptHeaderLocaleResolver();
        resolver.setDefaultLocale(DEFAULT_LANG);
        return resolver;
    }
}
